package com.winter.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 描述：系统相关信息
 *
 * @author He Yong
 * @version 1.0
 * @since 23/03/2018
 */
public class SystemInfo {
    /**
     * 运行环境获取
     * -Dkaopu.run.env=develop
     */
    public static final RunEnv RUN_ENV = RunEnv.valueOf(System.getProperty("kaopu.run.env", "develop"));


    public enum RunEnv {
        /**
         * 开发环境
         */
        develop("(开发)"),
        /**
         * 测试环境
         */
        test("(测试)"),
        /**
         * 生产环境
         */
        product("");

        String desc;

        RunEnv(String desc) {
            this.desc = desc;
        }

        public String getDesc() {
            return desc;
        }
    }

    private static final String COMPUTER_NAME = "COMPUTERNAME";

    /**
     * 启动时获取主机信息
     */
    public static final String HOSTNAME = getHostName();


    public static String getHostNameForLinux() {
        try {
            return (InetAddress.getLocalHost()).getHostName();
        } catch (UnknownHostException uhe) {
            // host = "hostname: hostname"
            String host = uhe.getMessage();
            if (host != null) {
                int colon = host.indexOf(':');
                if (colon > 0) {
                    return host.substring(0, colon);
                }
            }
            return "UnknownHost";
        }
    }

    public static String getHostName() {
        if (System.getenv(COMPUTER_NAME) != null) {
            return System.getenv(COMPUTER_NAME);
        } else {
            return getHostNameForLinux();
        }
    }
}
